﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireBias.Models;
using WinNemoLib;

using System.Net;
using System.IO;
using System.Text;

namespace TireBias.Controllers
{
    public class TirePLNController : Controller
    {
        //
        // GET: /TirePLN/

       public ActionResult Index(string dtPlan,string bias)
       {   
            if (StrUtil.IsEmpty(bias)) bias = "2";
            if (StrUtil.IsEmpty(dtPlan)) dtPlan = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
            ViewBag.dtPlan = dtPlan;            
            ViewBag.Biasno = bias;
         
            List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
            BIASPLANModel mdSearch = new BIASPLANModel();
            list = mdSearch.SEL_UNLOCK(dtPlan, bias);
            return View(list);
        
        }

       public ActionResult ViewPlan(string dtPlan, string bias)
       {
           if (StrUtil.IsEmpty(bias)) bias = "2";
           if (StrUtil.IsEmpty(dtPlan)) dtPlan = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
           ViewBag.dtPlan = dtPlan;
           ViewBag.Biasno = bias;

           List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
           BIASPLANModel mdSearch = new BIASPLANModel();
           list = mdSearch.GET_TOPCORDPLN_ACT("", "15", bias, "A", "", false, false);


           return View(list);

       }

       public JsonResult unLock(string sel,string bias)
       {
          string[] seqs = sel.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
          ViewBag.dtPlan = seqs[0];
          ViewBag.Biasno = bias;  
          BIASPLANModel mdSearch = new BIASPLANModel();

          if (mdSearch.UPDATE(User.Identity.Name.ToString(), "U", sel,"",bias))
          {
             return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
          }
          else
             return Json(new { success = 0, message = "ไม่สามารถทำงาน" });
       }
       public JsonResult GET_CODE(string code = "", string sizes = "", string sel = "tirecode")
       {

          string[] list;
          TopCordBomModel mdSearch = new TopCordBomModel();
          list = mdSearch.LIST_TOPCORDBOM(code,sizes,"",sel);
          return Json(list);
       }



       public JsonResult LockAll(string dtPlan,string bias)
       {

          BIASPLANModel mdSearch = new BIASPLANModel();

          if (mdSearch.UPDATE(User.Identity.Name.ToString(), "L", "", dtPlan,bias))
          {
             return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
          }
          else
             return Json(new { success = 0, message = "ไม่สามารถทำงาน" });
       }


       public ActionResult unLockTopping(string dtPlan, string bias)
        {
            if (StrUtil.IsEmpty(dtPlan)) return View( new BIASPLANModel.BIASPLAN_DS());
            if (StrUtil.IsEmpty(bias)) return View(new BIASPLANModel.BIASPLAN_DS());
               
           ViewBag.dtPlan = dtPlan;
           ViewBag.Biasno = bias;
           List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
           BIASPLANModel mdSearch = new BIASPLANModel();
           list = mdSearch.SEL_UNLOCK(dtPlan, bias);

           return View(list);
        }

        public JsonResult Add_Edit_UNLock(List<BIASPLANModel.BIASPLAN_DS> theData,string dtPlan, string bias)
        {
           if (theData == null)
           {
              return Json(new { success = "0", message = "ระบุค่าไม่ครบ" });
           }
           Boolean bln = true;  
           
           BIASPLANModel tp = new BIASPLANModel();
           int itemno = tp.GET_SEQ(dtPlan, bias);
           StringBuilder nofile = new StringBuilder("");
           List<TopCordBomModel.TIRESTDS_DS> listfile = new List<TopCordBomModel.TIRESTDS_DS>();

           using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
           {
              cnn.Open();
              cnn.BeginTransaction();
            
         
              if (!tp.DEL_DATA_NO_ACTIVE(cnn,dtPlan,bias))
              {
                 cnn.RollbackTransaction();
                 bln = false;
                 goto ExitProcess;
              }
              if (theData == null)
              {
                 cnn.CommitTransaction();
                 return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
              }

                int row=0;
              foreach (BIASPLANModel.BIASPLAN_DS incom in theData)
              {
                 TopCordBomModel chk = new TopCordBomModel();
                 List<TopCordBomModel.TIRESTDS_DS> list = new List<TopCordBomModel.TIRESTDS_DS>();
                 list = chk.SEL_TOPCORDBOM(incom._Topcrdcd,incom._Size,incom._Forming);
                 if (list.Count() <= 0)
                 {
                    return Json(new { success = 0
                        , message = "ไม่พบข้อมูลในระบบ " + incom._Topcrdcd + "/" + incom._Size + "/" + incom._Forming, rows =row ,item=0 , list = listfile});
                 }


                 string[] arr = new string[list.Count()];

                  int inx = 0;
                 foreach (TopCordBomModel.TIRESTDS_DS item in list)
                 {
                     arr[inx] = item._Ply;
                     inx = inx + 1;
                 }
                 if (StrUtil.IsNotEmpty(incom._P1) && (!arr.Contains("1")))
                 {
                     return Json(new { success = 0, message = "ไม่พบข้อมูลในระบบ P1", rows = row, item = 2, list = listfile });                        
                 }
                 if (StrUtil.IsNotEmpty(incom._P2) && (!arr.Contains("2")))
                 {
                     return Json(new { success = 0, message = "ไม่พบข้อมูลในระบบ P2", rows = row, item = 3, list = listfile });
                 }
                 if (StrUtil.IsNotEmpty(incom._P3) && (!arr.Contains("3")))
                 {
                     return Json(new { success = 0, message = "ไม่พบข้อมูลในระบบ P3", rows = row, item = 4, list = listfile });
                 }
                 if (StrUtil.IsNotEmpty(incom._P4) && (!arr.Contains("4")))
                 {
                     return Json(new { success = 0, message = "ไม่พบข้อมูลในระบบ P4", rows = row, item = 5, list = listfile });
                 }
                 incom._Itemno = itemno.ToString();
                 incom._Plants = (StrUtil.IsEmpty(incom._Plants) ? "U" : incom._Plants);


                 if (!tp.Ins_Data(cnn, incom, ""))
                 {
                    cnn.RollbackTransaction();
                    bln = false;
                    goto ExitProcess;
                 }
                 if (GET_File(list[0]._SubFd, list[0]._Filenm) == false)
                 {
                     listfile.Add(list[0]);
                     nofile.AppendLine("<br /> " + incom._Topcrdcd + "/" + incom._Size + "/" + incom._Forming);
                 }
                 itemno = itemno + 10;
                  row=row+1;
              }
              cnn.CommitTransaction();
           }

        ExitProcess:


           if (bln)
           {
               if (nofile.ToString().Length > 0)
               {
                   return Json(new { success = 0, message = "บันทึกเรียบร้อย", list = listfile });
               }
               return Json(new { success = 1, message = "บันทึกเรียบร้อย", list = listfile });
           }
           else
              return Json(new { success = 0, message = "ไม่สามารถทำงาน" });

        }

        public Boolean GET_File(string sub, string nm)
        {
            var address = Dns.GetHostAddresses(Nemo.GetINI("File", "DNS"))[0];
            string path = @"\\" + address + @"\" + Nemo.GetINI("File", "FilePath");


            try
            {

                if (!WinNemoLib.DriveMapper.GetConnected(null, path))
                {
                    DriveMapper.MapDrive(null, path, Nemo.GetINI("File", "USER"), Nemo.GetINI("File", "PWD"));
                }
                string file = path + @"\" + sub + @"\" + nm;
                FileInfo infile = new FileInfo(file);
                if (!infile.Exists)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                return false;
                throw;
            }
  
        }


        public JsonResult Get_USE_ACTIVE(string dtPlan, string bias)
        {
           List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
           BIASPLANModel mdSearch = new BIASPLANModel();
           list = mdSearch.GET_TOPCORDPLN_ACT("","15",bias,"A","",false,false);
           return Json(list);
        }
        public JsonResult Import()
        {
           string dtPlan = Request["dtPlan"].ToString();
           string biasno = Request["biasno"].ToString();
       
           List<BIASPLANModel.BIASPLAN_DS> list = new List<BIASPLANModel.BIASPLAN_DS>();
           BIASPLANModel mdSearch = new BIASPLANModel();
           list = mdSearch.GET_TOPCORDPLN_ACT(dtPlan,"" ,biasno,"","",false,false);
           return Json(list);
        }

       
    }
}
